/* eslint-disable space-before-function-paren */
<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employee" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getSimpleEmployee } from '@/api/employee'
import { addDepartment, getDepartDetail, updateDepartDetail, getDepartmentList } from '@/api/departments'
export default {
  name: 'DeptDialog',
  components: {},
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    // eslint-disable-next-line space-before-function-paren
    const checkCode = async (res, value, callback) => {
      await this.getDepartmentList()
      if (this.isEdit) {
        // 编辑部门编号的处理
        this.originList = this.originList.filter(item => item.id !== this.id)
      }
      // 添加部门编号的处理
      if (this.originList.some(({ code }) => code === value)) {
        callback(new Error(`部门编码${value}已存在`))
      } else {
        callback()
      }
    }
    // eslint-disable-next-line space-before-function-paren
    const checkName = async (res, value, callback) => {
      await this.getDepartmentList()
      if (this.isEdit) {
        // 编辑部门编号的处理
        // 当前的 id 的对象中的 pid 就是父级部门
        // 从数组中找到该对象，从对象中拿到 pid
        const pid = this.originList.find(item => item.id === this.id).pid
        // 从数组中在筛选出 pid 相同的的数据，排除当前数据，就是同级部门的数据
        this.originList = this.originList.filter(item => item.pid === pid && item.id !== this.id)
      } else {
        // 添加部门编号的处理
        this.originList = this.originList.filter(item => item.pid === this.id)
      }
      if (this.originList.some(({ name }) => name === value)) {
        callback(new Error(`部门名称${value}已存在`))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employee: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '输入的部门名称长度为 2 ~ 10 位', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '输入的部门名称长度为 2 ~ 10 位', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门描述', trigger: 'blur' },
          { min: 2, max: 300, message: '输入的部门名称长度为 2 ~ 10 位', trigger: 'blur' }
        ]
      },
      originList: [] // 部门列表
    }
  },
  created() {
    this.getSimpleEmployee()
    this.getDepartDetail()
  },
  methods: {
    // 获取用户信息
    async getSimpleEmployee() {
      const { data } = await getSimpleEmployee()
      this.employee = data
    },
    // 根据 id 获取部门详情
    async getDepartDetail() {
      if (!this.isEdit) return
      const res = await getDepartDetail(this.id)
      this.form = res.data
    },
    // 获取部门列表信息
    async getDepartmentList() {
      const { data } = await getDepartmentList()
      this.originList = data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
    },
    // 确定按钮
    async hSubmit() {
      const res = await this.$refs.deptForm.validate().catch(e => e)
      if (!res) return
      // // 判断 isEdit 的状态
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    // 取消按钮
    hCancel() {
      // this.$refs.deptForm.resetFields()
      this.$emit('Close')
    },
    // 新增部门
    async doAdd() {
      try {
        // 发送请求添加部门
        const res = await addDepartment({ ...this.form, pid: this.id })
        // 提醒用户添加成功
        this.$message.success(res.message)
        // 关闭弹层，更新数据
        this.$parent.$parent.success()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 编辑部门详情
    async doEdit() {
      try {
        // 发送请求添加部门
        const res = await updateDepartDetail(this.form)
        // 提醒用户添加成功
        this.$message.success(res.message)
        // 关闭弹层，更新数据
        this.$parent.$parent.success()
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>

<style lang='less' scoped>
</style>
